<?php
class comment{
	function add(&$parent_id,&$content,&$user_name,&$task_id){
		global $dbs , $user,$encrypt  ;
		$parent_id_code = $encrypt -> code($parent_id);
		
		$array['parent_id']		= $parent_id;
		$array['content']		= $content;
		$array['ip']			= ip();
		$array['time']			= date('Y-m-d H:i:s',time());
		
		if( !($user_name == ''  && $task_id == 0)  )	{
			$array['task_id']		= $task_id ;
			$array['user_name']		= $user_name;
			
			$check = $dbs -> select('SELECT * FROM `'.DB_PRE.'task_invite` WHERE `task_id` = '. $task_id .' and `invite` = "'. $user_name .'" limit 0,1 ');
			if( count($check) > 0)
				$comment_id = $dbs -> insert( DB_PRE .'comments_judge' , $array ,'id' );
			else
				exit('<li class="hidden noExist">伪造的评委姓名.您不能在此页面留言</li>');
				
		}else{
			$array['user_id']		= $user -> id();
			
			// 防止灌水
			if(! self::checkTimeInterval( $array['user_id'] )){
				exit('<li class="noExist">您的操作过于频繁.</li>');
			};			
			
			$comment_id = $dbs -> insert( DB_PRE .'comments' , $array ,'id' );
			$face = getFace($user -> id());
			$userInfo = $user -> getUser($user -> id());
		}
		
		$html .='<li class="hidden">';
			if( !($user_name == ''  && $task_id == 0)  )	{
				//$html .='<a><img src="'. THEME_PATH .'images/judgeFace.jpg" /></a>';
				$html .='<div class="userInfo"><a class="beautyImg"><img src="'. THEME_PATH .'images/judgeFace.jpg" /></a></div>'
			.'<div class="userName"><b>'. lang_judge .'</b><span class="time">'.date('Y-m-d H:i',time()).'</span>'.lang_say.'：';			
				//$html .='<div class="operate"><a class="del" href="./?option=commentsAction&type=action&do=del&parent_id='.$parent_id_code.'&comment_id='.$encrypt -> code($comment_id).'">'.lang_del.'</a></div>';				
				$html .='</div><div class="contents">'. htmlShow($content,'<b><a><br>') .'</div>';	
			}else{
				//$html .='<a target="_blank" title="'.$userInfo['userName'].'" href="./?option=home&id='.$list['user_id'].'"><img src="'. getFace($list['user_id']) .'" /></a>';	
				$html .='<div class="userInfo"><span class="beautyImg" target="_blank" title="'.$userInfo['userName'].'" href="'.urlRewrite('option=home&id='.$list['user_id']).'"><img src="'. $face .'" /></span></div>'
			.'<div class="userName"><a target="_blank" title="'.$userInfo['userName'].'" href="'.urlRewrite('option=home&id='.$list['user_id']).'">'.htmlShow($userInfo['nickName']).'</a><span class="time">'.date('Y-m-d H:i',time()).'</span>'.lang_say.'：';
			
				$html .='<div class="operate"><a class="del" href="'.WEB_ROOT_PATH.'?option=commentsAction&type=action&do=del&parent_id='.$parent_id_code.'&comment_id='.$encrypt -> code($comment_id).'">'.lang_del.'</a></div>';				
				$html .='</div><div class="contents">'. htmlShow($content,'<b><a><br>') .'</div>';
			}
		$html .='<i></i></li><hr />';
		
		$array = '';
		$sql = 'SELECT `comments` FROM `'.DB_PRE .'file` WHERE `id` = '.  $parent_id ;
		$lists = $dbs -> select($sql);
		$array['comments'] =  $lists[0]['comments'] + 1;
		$dbs -> update( DB_PRE .'file' , $array , ' `id` = '. $parent_id );
		
		exit('{"status":"ok","msg":'. json_encode(checkBadWords($html)) .'}');
		exit(checkBadWords($html));
	
	}	
	
	function getList(&$parent_id , &$user_name, &$task_id  ){
		global $dbs,$page,$user , $encrypt;
		$parent_id_code = $encrypt -> code($parent_id);
	
		if( '' == $user_name )
			$sqlCount = '(SELECT `id`,`parent_id`,`content`,`user_id`,`time` FROM `'. DB_PRE .'comments` WHERE `parent_id` = '.$parent_id.') union all (SELECT `id`,`parent_id`,`content`,`user_id`,`time`  FROM `'. DB_PRE .'comments_judge` WHERE `parent_id` = '.$parent_id.') ORDER BY `time` DESC';
		else
			$sqlCount = 'SELECT * FROM `'. DB_PRE .'comments_judge` WHERE `parent_id` = '.$parent_id.' and `user_name` = "'. $user_name .'" and `task_id` = '. $task_id .' ORDER BY `id` DESC';
		
		
		$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE * 2 ) .' , '. PAGE_SIZE * 2;
		$lists	= $dbs -> select($sqlCount);
		$total	= count($lists);		
		$lists  = $dbs -> select($sql);
		$html .='';
		$user = new users();
		foreach($lists as $list){			
			$userInfo = $user -> getUser($list['user_id']);
			
			
			$html .='<li>
				<div class="userInfo">';
				if( $list['user_id'] != 0 )
					$html .='<a target="_blank" title="'.$userInfo['userName'].'" href="'.urlRewrite('option=home&id='.$list['user_id']).'"><span class="beautyImg"><img src="'. getFace($list['user_id']) .'" width="'.USERFACE_SMALL_WIDTH.'" height="'.USERFACE_SMALL_HEIGHT.'" /></span></a>';
				else
					$html .='<span class="beautyImg"><img src="'. THEME_PATH .'images/judgeFace.jpg" /></span>';
				$html.='</div>
				<div class="userName">';
				if( $list['user_id'] != 0 )
					$html.='<a target="_blank" title="'.$userInfo['userName'].'" href="'.urlRewrite('option=home&id='.$list['user_id']).'">'.htmlShow($userInfo['nickName']).'</a>';
				else
					$html.= '<b>'. lang_judge .'</b>' ;
					
				$html.='<span class="time">'.date('Y-m-d H:i',strtotime($list['time'])).'</span>'.lang_say.'：';
			
			if( $list['user_id'] == $user -> id() || $user-> powder() >= 9 ){
				$commentDelType = $list['user_id'] == 0 ? '&commentDelType=judge' : '' ;
				$html .='<div class="operate"><a class="del" href="'.WEB_ROOT_PATH.'?option=commentsAction&type=action&do=del&parent_id='.$parent_id_code.'&comment_id='.$encrypt -> code($list['id']).''. $commentDelType .'">'.lang_del.'</a></div>';	
			}
				
			$html .='</div>'
				.'<div class="contents">'. checkBadWords( htmlShow($list['content'], '<b><a><br>')) .'</div>';
		    $html .='<i></i></li><hr />';			
		}
		$html .= '<div class="clear"></div>';
		
		$html .= getPages($page,$total,getPageFileUrl().'&id='.$parent_id_code,PAGE_SIZE* 2, PAGE_LEN);
		return $html;
	
	}
	
	
	function getAll( &$user_id ){
		global $dbs,$page,$user , $encrypt;
		
		if( '' == $user_id ){
			$sqlCount = '(SELECT `id`,`parent_id`,`content`,`user_id`,`time` FROM `'. DB_PRE .'comments` ) union all (SELECT `id`,`parent_id`,`content`,`user_id`,`time`  FROM `'. DB_PRE .'comments_judge` ) ORDER BY `time` DESC';
		}else if( 0 == $user_id ){
			$sqlCount = 'SELECT `id`,`parent_id`,`content`,`user_id`,`time`  FROM `'. DB_PRE .'comments_judge` WHERE `user_id` = 0 ORDER BY `time` DESC';
		}else{
			$sqlCount = 'SELECT `id`,`parent_id`,`content`,`user_id`,`time` FROM `'. DB_PRE .'comments` WHERE `user_id` = '.(int)$user_id.' ORDER BY `id` DESC';
		}
		$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE * 10 ) .' , '. PAGE_SIZE * 10;
		$lists	= $dbs -> select($sqlCount);
		$total	= count($lists);		
		$lists  = $dbs -> select($sql);
		$html ='<ul class="list">';
		foreach($lists as $list){			
			$userInfo = $user -> getUser($list['user_id']);
			$html .='<li>
				<div class="userInfo">';
				if( $list['user_id'] != 0 )
					$html .='<a target="_blank" title="'.$userInfo['userName'].'" href="'.urlRewrite('option=home&id='.$list['user_id']).'"><img src="'.getFace($list['user_id']) .'" /></a>';
				else
					$html .='<img src="'. THEME_PATH .'images/judgeFace.jpg" />';
				
				$html.='</div>
				<div class="userName">';
				if( $list['user_id'] != 0 )
					$html.='<a target="_blank" title="'.$userInfo['userName'].'" href="'.urlRewrite('option=home&id='.$list['user_id']).'">'.htmlShow($userInfo['nickName']).'</a>';
				else
					$html.= '<b>'. lang_judge .'</b>' ;
					
				$html.='<span class="time">'.date('Y-m-d H:i',strtotime($list['time'])).'</span>'.lang_say.'：';
			
			if( $list['user_id'] === $user -> id() || $user-> powder() >= 9 ){
				$commentDelType = $list['user_id'] == 0 ? '&commentDelType=judge' : '' ;
				$html .='<div class="operate"><a class="del" href="'.WEB_ROOT_PATH.'?option=commentsAction&type=action&do=del&parent_id='.$parent_id_code.'&comment_id='.$encrypt -> code($list['id']).''. $commentDelType .'">'.lang_del.'</a></div>';//<input type="checkbox" name="" />
			}
			$html .='</div>
				<div class="content">'. checkBadWords( htmlShow($list['content'], '<b><a><br>')) ;
			$html .='<p>';
			if( $user_id == '' ) 
				$html .='<a href="'.WEB_ROOT_PATH.'?option=admin.comment&user_id='. $list['user_id'] .'">此人的所有留言</a> | ';
			$html .='<a href="'. urlRewrite('option=media&id='.$encrypt->code($list['parent_id'])) .'" target="_blank">跳转到留言页面</a></p>';			
			$html .='</div>';
		    $html .='<i></i></li><hr />';			
		}
		$html .= '<div class="clear"></div>';
		$html .= '</ul>';
		$html .= getPages($page,$total,getPageFileUrl().'&user_id='. $user_id .'&page='.$page,PAGE_SIZE* 10, PAGE_LEN);
		return $html;
	
	}

	function getCount($parent_id){		
		global $dbs;
		$sql = 'SELECT count(*) as `counts` FROM `'. DB_PRE .'comments` WHERE `parent_id` = '.$parent_id ;
		$count = $dbs -> select($sql);
		$count = $count[0]['counts'];
		return $count;
	}
	
	function del( &$parent_id,&$comment_id , &$commentDelType){
		global $dbs, $user,$encrypt;
		//$sql = 'SELECT count(*) as `counts` FROM `'. DB_PRE .'comments` WHERE `id` = '. $comment_id .' and `parent_id` = '.$parent_id .' and `user_id` ='. $user ->id()  ;
		$dbTable = $commentDelType == '' ? '' : '_'.$commentDelType ;

		if( $user -> powder() >= 9  ){
			$dbs -> delete(DB_PRE .'comments'.$dbTable , '`id` = '. $comment_id );
		}else{
			$dbs -> delete(DB_PRE .'comments'.$dbTable , '`id` = '. $comment_id .' and `parent_id` = '.$parent_id .' and `user_id` ='. $user -> id() );
		}
		
		$array['comments'] =  self::getCount($parent_id) ;
		$dbs -> update( DB_PRE .'file' , $array , ' `id` = '. $parent_id );
		
		
		exit('{"status":"ok","msg":"'.lang_delete_success.$dbTable .'"}');
	}
	
	function checkTimeInterval( &$user_id ){
		global $dbs;
		$list = $dbs -> select('SELECT * FROM `'.DB_PRE.'comments` WHERE `user_id` ='. $user_id .' ORDER BY `id` DESC limit 0,1');
		$list = $list[0];
		if( count($list) == 0 )
			return true ;
		// 限制留言时间间隔
		if( (float)date('YmdHis',time()) - (float)date('YmdHis',strtotime($list['time'])) < COMMENT_INTERVAL )  
			return false;
		return true ;
	}
	
}
?>